package com.woniu.secondhand.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.common.entity.ResultCode;
import com.woniu.common.exception.LifeException;
import com.woniu.secondhand.constant.SecondhandMessageConstant;
import com.woniu.secondhand.entity.dto.MessageAddDTO;
import com.woniu.secondhand.mapper.SecondhandMessageMapper;
import com.woniu.secondhand.mapper.SecondhandOrderMapper;
import com.woniu.secondhand.mapper.SecondhandProductMapper;
import com.woniu.secondhand.pojo.SecondhandComment;
import com.woniu.secondhand.pojo.SecondhandMessage;
import com.woniu.secondhand.pojo.SecondhandOrder;
import com.woniu.secondhand.pojo.SecondhandProduct;
import com.woniu.secondhand.service.ISecondhandCommentService;
import com.woniu.secondhand.service.ISecondhandMessageService;
import com.woniu.user.feignClient.UserFeignClient;
import com.woniu.user.pojo.User;
import com.woniu.user.vo.UserLoginVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author HMMT
 * @since 2023-05-20
 */
@Service
public class SecondhandMessageServiceImpl extends ServiceImpl<SecondhandMessageMapper, SecondhandMessage> implements ISecondhandMessageService {

    @Autowired
    private SecondhandMessageMapper messageMapper;
    @Autowired
    private SecondhandProductMapper productMapper;
    @Autowired
    private SecondhandOrderMapper orderMapper;
    @Autowired
    private ISecondhandCommentService commentService;
    @Autowired
    private UserFeignClient userFeignClient;
    @Autowired
    private RedisTemplate redisTemplate;

    public static final byte SYSTEM_MESSAGE = 0;
    public static final byte CONTACT_SUCCESS_MESSAGE = 1;
    public static final byte BUY_SUCCESS_UNPAID_MESSAGE = 2;
    public static final byte BUY_SUCCESS_MESSAGE = 3;
    public static final byte DELIVER_SUCCESS_MESSAGE = 4;
    public static final byte RECEIVE_SUCCESS_MESSAGE = 5;
    public static final byte COMMENT_SUCCESS_MESSAGE = 6;


    @Override
    public boolean addMessage(MessageAddDTO messageAddDTO, UserLoginVO userLoginVO) {
        switch (messageAddDTO.getMessageType()) {
            case SYSTEM_MESSAGE:
                addSystemMessage(messageAddDTO);
                break;
            case CONTACT_SUCCESS_MESSAGE:
                addContactSuccessMessage(userLoginVO, messageAddDTO.getProductId());
                break;
            case BUY_SUCCESS_UNPAID_MESSAGE:
                addBuySuccessUnpaidMessage(userLoginVO, messageAddDTO.getOrderId());
                break;
            case BUY_SUCCESS_MESSAGE:
                addBuySuccessMessage(userLoginVO, messageAddDTO.getOrderId());
                break;
            case DELIVER_SUCCESS_MESSAGE:
                addDeliverSuccessMessage(userLoginVO, messageAddDTO.getOrderId());
                break;
            case RECEIVE_SUCCESS_MESSAGE:
                addReceiveSuccessMessage(userLoginVO, messageAddDTO.getOrderId());
                break;
            case COMMENT_SUCCESS_MESSAGE:
                addCommentSuccessMessage(userLoginVO, messageAddDTO.getProductId());
                break;
            default:
                throw new LifeException(ResultCode.MESSAGE_TYPE_NOT_EXIST.getMessage(), ResultCode.MESSAGE_TYPE_NOT_EXIST);
        }
        return true;
    }

    @Override
    public void addBuySuccessMessage(MessageAddDTO messageAddDTO, Integer consumerId) {
                /*
            是买家成功付钱 所以是发送给卖家的信息
         */
        SecondhandMessage message = new SecondhandMessage();
        SecondhandOrder order = orderMapper.selectById(messageAddDTO.getOrderId());
        User sellUser = userFeignClient.getUser(order.getProducerId()).getData();
        SecondhandProduct product = productMapper.selectById(order.getProductId());
        message.setUserId(sellUser.getId());
        message.setUserName(sellUser.getName());
        message.setTitle(SecondhandMessageConstant.BUY_SUCCESS_TITLE);
        message.setContent(product.getTitle() + SecondhandMessageConstant.BUY_SUCCESS_CONTENT);
        message.setType(SecondhandMessageConstant.BUY_SUCCESS_MESSAGE);
        message.setStatus(SecondhandMessageConstant.MESSAGE_UNREAD);
        messageMapper.insert(message);
    }

    @Override
    public void readMessage(String messageId) {
        SecondhandMessage message = messageMapper.selectById(messageId);
        message.setStatus(SecondhandMessageConstant.MESSAGE_READIED);
        messageMapper.insert(message);
    }


    private void addSystemMessage(MessageAddDTO messageAddDTO) {
        /*
          1.设定这条系统消息的属性
          2.遍历出所有user对象 并且将user对象中的id存入idList集合中
          3.保存这条消息进入数据库
          4.取到这条消息的id
          5.在redis中创建一个以这条消息的id为key 以全体用户的id为value的set集合
         */
        SecondhandMessage message = new SecondhandMessage();
        message.setUserId(SecondhandMessageConstant.SYSTEM_MESSAGE_USERID);
        message.setUserName(SecondhandMessageConstant.SYSTEM_MESSAGE_USERNAME);
        message.setTitle(messageAddDTO.getTitle());
        message.setContent(messageAddDTO.getContent());
        message.setType(SecondhandMessageConstant.SYSTEM_MESSAGE);
        message.setStatus(SecondhandMessageConstant.MESSAGE_UNREAD);
        List<User> userList = userFeignClient.listUser().getData();
        List<Integer> idList = new ArrayList<>();
        userList.forEach(s -> idList.add(s.getId()));
        messageMapper.insert(message);
        Integer messageId = message.getId();
        redisTemplate.opsForSet().add(SecondhandMessageConstant.SYSTEM_MESSAGE_REDIS + messageId, idList.toArray());
    }

    private void addContactSuccessMessage(UserLoginVO userLoginVO, Integer productId) {
        /*
            是买家成功联系卖家 所以是发送给卖家的信息
         */
        SecondhandMessage message = new SecondhandMessage();
        SecondhandProduct product = productMapper.selectById(productId);
        User sellUser = userFeignClient.getUser(product.getProducerId()).getData();
        message.setUserId(sellUser.getId());
        message.setUserName(sellUser.getName());
        message.setTitle(SecondhandMessageConstant.CONTACT_SUCCESS_TITLE);
        message.setContent(userLoginVO.getName() + SecondhandMessageConstant.CONTACT_SUCCESS_CONTENT1 + userLoginVO.getMobile()
                + SecondhandMessageConstant.CONTACT_SUCCESS_CONTENT2 + product.getTitle());
        message.setType(SecondhandMessageConstant.CONTACT_SUCCESS_MESSAGE);
        message.setStatus(SecondhandMessageConstant.MESSAGE_UNREAD);
        messageMapper.insert(message);
    }


    private void addBuySuccessUnpaidMessage(UserLoginVO userLoginVO, Integer orderId) {
        /*
            是买家成功付钱 所以是发送给卖家的信息
         */
        SecondhandMessage message = new SecondhandMessage();
        SecondhandOrder order = orderMapper.selectById(orderId);
        User sellUser = userFeignClient.getUser(order.getProducerId()).getData();
        SecondhandProduct product = productMapper.selectById(order.getProductId());
        message.setUserId(sellUser.getId());
        message.setUserName(sellUser.getName());
        message.setTitle(SecondhandMessageConstant.BUY_SUCCESS_UNPAID_TITLE);
        message.setContent(userLoginVO.getName() + SecondhandMessageConstant.BUY_SUCCESS_UNPAID_CONTENT + product.getTitle());
        message.setType(SecondhandMessageConstant.BUY_SUCCESS_MESSAGE);
        message.setStatus(SecondhandMessageConstant.MESSAGE_UNREAD);
        messageMapper.insert(message);
    }

    private void addBuySuccessMessage(UserLoginVO userLoginVO, Integer orderId) {
        /*
            是买家成功付钱 所以是发送给卖家的信息
         */
        SecondhandMessage message = new SecondhandMessage();
        SecondhandOrder order = orderMapper.selectById(orderId);
        User sellUser = userFeignClient.getUser(order.getProducerId()).getData();
        SecondhandProduct product = productMapper.selectById(order.getProductId());
        message.setUserId(sellUser.getId());
        message.setUserName(sellUser.getName());
        message.setTitle(userLoginVO.getName() + SecondhandMessageConstant.BUY_SUCCESS_TITLE);
        message.setContent(product.getTitle() + SecondhandMessageConstant.BUY_SUCCESS_CONTENT);
        message.setType(SecondhandMessageConstant.BUY_SUCCESS_MESSAGE);
        message.setStatus(SecondhandMessageConstant.MESSAGE_UNREAD);
        messageMapper.insert(message);
    }

    private void addDeliverSuccessMessage(UserLoginVO userLoginVO, Integer orderId) {
        /*
            是卖家成功发货 所以是给买家的信息
         */
        SecondhandMessage message = new SecondhandMessage();
        SecondhandOrder order = orderMapper.selectById(orderId);
        User user = userFeignClient.getUser(order.getConsumerId()).getData();
        SecondhandProduct product = productMapper.selectById(order.getProductId());
        message.setUserId(user.getId());
        message.setUserName(user.getName());
        message.setTitle(SecondhandMessageConstant.DELIVER_SUCCESS_TITLE);
        message.setContent(userLoginVO.getName() + SecondhandMessageConstant.DELIVER_SUCCESS_CONTENT + product.getTitle());
        message.setType(SecondhandMessageConstant.DELIVER_SUCCESS_MESSAGE);
        message.setStatus(SecondhandMessageConstant.MESSAGE_UNREAD);
        messageMapper.insert(message);
    }

    private void addReceiveSuccessMessage(UserLoginVO userLoginVO, Integer orderId) {
        /*
            是买家收货成功 所以是给卖家的信息
         */
        SecondhandMessage message = new SecondhandMessage();
        SecondhandOrder order = orderMapper.selectById(orderId);
        User sellUser = userFeignClient.getUser(order.getProducerId()).getData();
        SecondhandProduct product = productMapper.selectById(order.getProductId());
        message.setUserId(sellUser.getId());
        message.setUserName(sellUser.getName());
        message.setTitle(SecondhandMessageConstant.RECEIVE_SUCCESS_TITLE);
        message.setContent(product.getTitle() + SecondhandMessageConstant.RECEIVE_SUCCESS_CONTENT + userLoginVO.getName());
        message.setType(SecondhandMessageConstant.RECEIVE_SUCCESS_MESSAGE);
        message.setStatus(SecondhandMessageConstant.MESSAGE_UNREAD);
        messageMapper.insert(message);
    }

    private void addCommentSuccessMessage(UserLoginVO userLoginVO, Integer productId) {
        /*
            是买家评价成功 所以是给卖家的信息
         */
        SecondhandMessage message = new SecondhandMessage();
        SecondhandProduct product = productMapper.selectById(productId);
        User sellUser = userFeignClient.getUser(product.getProducerId()).getData();
        SecondhandComment comment = commentService.getByUserIdAndProductId(userLoginVO.getId(), product.getId());
        message.setUserId(sellUser.getId());
        message.setUserName(sellUser.getName());
        message.setTitle(SecondhandMessageConstant.COMMENT_SUCCESS_TITLE);
        message.setContent(userLoginVO.getName() + SecondhandMessageConstant.COMMENT_SUCCESS_CONTENT + comment.getContent());
        message.setType(SecondhandMessageConstant.RECEIVE_SUCCESS_MESSAGE);
        message.setStatus(SecondhandMessageConstant.MESSAGE_UNREAD);
        messageMapper.insert(message);
    }
}

